define(function(require, exports, module) {
    require("common/umei.dev");
    // var page = require("common/umei.pagination");
    // var umei = require("common/umei.umei");
    // var umei = require("common/umei.dialog");
    // var umei = require("common/umei.form");
    var code_form = require("../../tpl/code_table_form.html");
    umei.url = {
	loadList : "loadCodetableList.do",// 列表
	loadSubList : "loadCodetableSubList.do", // 子码表列表
	codeDelete : "user_delete.do",// 删除
	codetableAdd : "codetable_add.do",// 添加
	codetableUpdate : "codetable_update.do"// 修改
    }
    umei.searchParam = "";
    initPage(1);
    // 搜索
    umei.initForm({
	formObj : $("#searchform"),autocommit : "true", commiturl : umei.url.loadList, fun : function(response) {
	    umei.searchParam = umei.serialize($("#searchform"));
	    fillPage(response);
	}
    });
    // 删除
    $("#btn-del").click(function() {
	if (checkSelected()) {
	    umei.confirm("确定要删除吗？", function() {
		var data = $("#form-list").serialize();
		umei.postRequest({
		    async : true, url : umei.url.userDelete, data : data, fun : function(response) {
			initPage(umei.pageNo);
			umei.tipMsg({
			    type : "success", content : "删除成功", timeout : 1500
			});
		    }
		});
	    });
	}
    });

    $(".codetable-add").click(function() {
	showDialog();
    });

    $(document).on("click", ".btn-edit", function() {
	showDialog($(this));
    });

    // 校验是否选择了列列
    function checkSelected() {
	var checkInputs = $("#pager_content tr td input[name='userIds']:checked");
	if (checkInputs.length == 0) {
	    umei.tipMsg({
		type : "warn", content : "请选择要操作的选项", timeout : 1500
	    });
	    return false;
	} else {
	    return true;
	}
    }

    // 初始化页面列表
    function initPage(pageNo) {
	umei.pageNo = pageNo;
	var data = umei.searchParam + "&pageNo=" + pageNo;
	if (umei.pid != null) {
	    data = data + "&pid=" + umei.pid;
	}
	var url = umei.url.loadList
	if (umei.pid != null) {
	    url = umei.url.loadSubList
	}
	umei.postRequest({
	    async : true, url : url, data : data, fun : function(response) {
		fillPage(response);
	    }
	});
    }

    function fillPage(response) {
	var data = response.data;
	var simplePage = data.page;
	var list = data.list;
	var simplePage = data.page;
	umei.page = simplePage;
	$("#pager_content tr:gt(0)").remove();
	if (list.length > 0) {
	    for (var i = 0, _len = list.length, d; i < _len, d = list[i]; i++) {
		var edit = "";
		if (umei.op.showEidt == "true") {
		    edit = edit + '<a href="javascript:void(0)" title="修改" class="btn-edit" id=' + d.id + '><i class="icon i-edit"></i></a>&nbsp;&nbsp;&nbsp;&nbsp;';
		}
		if (umei.op.addSub == "true") {
		    edit = edit + '<a href="codetable_sub_list.do?id=' + d.id + '" title="查看" ><i class="icon i-preview"></i></a>';
		}
		$('<tr><td class="c_code">' + d.code + '</td><td class="c_name">' + d.name + '</td><td class="c_sort">' + d.sort + '</td><td>' + d.lastUpdateTime + '</td><td>' + edit + '</td></tr>')
			.appendTo($("#pager_content"));
	    }
	} else {
	    $('<tr><td  class="nodata" colspan="100">没有数据</td></tr>').appendTo($("#pager_content"));
	}
	umei.pagination({
	    pagePanelId : "pager", pageObj : simplePage, fun : initPage
	});
    }

    // 显示弹出框
    function showDialog(obj) {
	var title = "新增码表";
	$("#code-form input").val("");
	var content = code_form;
	umei.form_dialog = dialog({
	    title : title, content : content, drag : true, okValue : '确 定', ok : function() {
		saveCode(obj);
		return false;
	    }, cancelValue : '取消', cancel : function() {}
	});
	umei.form_dialog.showModal();
	if (umei.pid != null) {
	    $("#code-form input[name='pid']").val(umei.pid);
	}
	var trlength = $("#pager_content tr:gt(0)").length;
	$("#code-form input[name='sort']").val(trlength + 1 + (umei.page.page - 1) * umei.page.pageSize);
	if (obj != null) {
	    title = "修改码表";
	    var id = obj.attr("id")
	    var code = obj.parent().parent().find(".c_code").text();
	    var name = obj.parent().parent().find(".c_name").text();
	    var sort = obj.parent().parent().find(".c_sort").text();
	    $("#code-form input[name='id']").val(id);
	    $("#code-form input[name='code']").val(code);
	    $("#code-form input[name='name']").val(name);
	    $("#code-form input[name='sort']").val(sort);
	}
	umei.initForm({
	    formObj : $("#code-form")
	});
    }

    function saveCode(obj) {
	if (!umei.verification({
	    formObj : $("#code-form"),
	    type:"inline"
	})) {
	    return;
	}
	var data = umei.serialize($("#code-form"));
	var url = umei.url.codetableAdd;
	if (obj != null) {
	    url = umei.url.codetableUpdate;
	}
	umei.postRequest({
	    async : true, url : url, data : data, fun : function(response) {
		umei.form_dialog.remove();
		umei.tipMsg({
		    type : "success", content : "操作成功", timeout : 1500
		});
		initPage(umei.pageNo);
	    }
	});
    }
});
